#include <gtk/gtk.h>
#include <sys/time.h>
#include <pthread.h>

int double_click_wait=0;
int queue_count=1;

void single_clicked_func()
{
	printf("Single click\n");
	return;
}

void double_clicked_func()
{
	struct timeval t;
	gettimeofday(&t, NULL);
    printf("%d\n%d\n", t.tv_sec, t.tv_usec);
    printf("Double clicked on tray icon\n");
	return;	
}

void* do_something_on_click(void* s_pointer)
{
	double_click_wait=1;
    printf("1\n");
    sleep(1);
    if(double_click_wait)
    	single_clicked_func();
	double_click_wait=0;
    printf("0\n");
    pthread_exit(NULL);
}

void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data)
{
      
      int rc;
      pthread_t pointer;
    
      if(queue_count)
      {
	      if(double_click_wait)
	      {
	      	double_click_wait=0;
	      	double_clicked_func();
	      }
	      else
	      {
	      	rc=pthread_create(&pointer, NULL, do_something_on_click, NULL);
	      }
	   }
	   else
	   {
	   	single_clicked_func();
	   }
}


void tray_icon_on_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
{
        printf("Right click: Popup menu\n");
}

static GtkStatusIcon *create_tray_icon()
{
        GtkStatusIcon *tray_icon;

        tray_icon = gtk_status_icon_new();
        g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(tray_icon_on_click), NULL);
        g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_icon_on_menu), NULL);
        gtk_status_icon_set_from_icon_name(tray_icon, GTK_STOCK_MEDIA_STOP);
        gtk_status_icon_set_tooltip(tray_icon, "Example Tray Icon");
        gtk_status_icon_set_visible(tray_icon, TRUE);

        return tray_icon;
}

int main(int argc, char **argv)
{
        GtkStatusIcon *tray_icon;

        gtk_init(&argc, &argv);
        tray_icon = create_tray_icon();
        gtk_main();

        return 0;
}

